package chuishop.yiqimaicai.controller;

import chuishop.yiqimaicai.pojo.Addgoods_body;
import chuishop.yiqimaicai.pojo.Orderpick;
import chuishop.yiqimaicai.service.OrderpickService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class OrderpickController {
    @Autowired
    private OrderpickService orderpickService;

    @RequestMapping("findAllOrderpick")
    @ResponseBody
    public PageInfo<Orderpick> findAllOrderpick(Integer pageNo, Integer pageSize){
        if(pageNo==null){
            pageNo=1;
        }
        if(pageSize==null){
            pageSize=5;
        }
        //启动分页
        PageHelper.startPage(pageNo,pageSize);

        List<Orderpick> list=orderpickService.findAllOrderpick();
        for(int i=0;i<list.size();i++) {
            System.out.println(list.get(i).getPid()+"\t"+list.get(i).getUid()+"\t"+list.get(i).getPname()+"\t"
                    +list.get(i).getEmail() +"\t"+list.get(i).getPickaddress());
        }
        PageInfo<Orderpick> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @RequestMapping("findOrderpickById")
    @ResponseBody
    public PageInfo<Orderpick> findOrderpickById(Integer pid,Integer pageNo, Integer pageSize){
        if(pageNo==null){
            pageNo=1;
        }
        if(pageSize==null){
            pageSize=5;
        }
        //启动分页
        PageHelper.startPage(pageNo,pageSize);

        List<Orderpick> list=orderpickService.findOrderpickById(pid);

        for(int i=0;i<list.size();i++) {
            System.out.println(list.get(i).getPid()+"\t"+list.get(i).getUid()+"\t"+list.get(i).getPname()+"\t"
                    +list.get(i).getEmail() +"\t"+list.get(i).getPickaddress());
        }
        PageInfo<Orderpick> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    //删除功能
    @RequestMapping("delOrderpick")
    @ResponseBody
    public Map<Object, Object> delOrderpick(String ids) {
        System.out.println(ids);

        String idstr[] = ids.split(",");
        for (String str : idstr) {
            System.out.println(str);
        }
        System.out.println(idstr.length);

        Map<Object, Object> result = new HashMap<>();
        boolean flag = orderpickService.delOrderpick(idstr);
        if (flag) {
            result.put("status", "200");
            result.put("msg", "删除成功");
        } else {
            result.put("status", "500");
            result.put("msg", "删除失败");
        }
        return result;
    }

    @RequestMapping("addOrderpick")
    @ResponseBody
    public Map<Object, Object> addOrderpick(@RequestBody Orderpick orderpick) {
        System.out.println(orderpick.getPid()+"\t"+orderpick.getPname()+"\t"+orderpick.getPickaddress());
        Map<Object, Object> result = new HashMap<>();
        boolean flag =orderpickService.addOrderpick(orderpick);
        if (flag) {
            result.put("status", "200");
            result.put("msg", "添加成功");
        } else {
            result.put("status", "500");
            result.put("msg", "添加失败");
        }
        return result;
    }

    @RequestMapping("updateOrderpick")
    @ResponseBody
    public Map<Object, Object> updateOrderpick(@RequestBody Orderpick orderpick) {
        System.out.println(orderpick.getPid()+"\t"+orderpick.getPname()+"\t"+orderpick.getPickaddress());
        Map<Object, Object> result = new HashMap<>();
        boolean flag =orderpickService.updateOrderpick(orderpick);
        if (flag) {
            result.put("status", "200");
            result.put("msg", "添加成功");
        } else {
            result.put("status", "500");
            result.put("msg", "添加失败");
        }
        return result;
    }

}
